package graphics.screens;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;

import boot.Boot;
import files.Files;
import function.Mp3File;
import function.NoDataException;
import function.search.EventListener;
import graphics.SearchChoiceInput2;

public class PawnScreen extends Canvas implements CommandListener {

	private static final String PAWNED_FOLDER = Files.BOOT_ROOT + "/Pawn";

	private SearchChoiceInput2 pawnChoiceInput;
	private Mp3File[] mp3List;
	private String[] mp3TitleList;
	private final Command cancelCommand = new Command("Cancel", Command.EXIT, 1);

	public PawnScreen() {
		addCommand(cancelCommand);
		setCommandListener(this);
		reboot();
	}

	protected void paint(Graphics g) {

		pawnChoiceInput.repaint(g);
	}

	public void commandAction(Command command, Displayable dis) {
		if (command == cancelCommand) {
			Boot.getInstance().readyToGo();
		}
	}

	public void keyPressed(int keyCode) {

		pawnChoiceInput.keyPressed(keyCode);
		repaint();
	}

	public void reboot() {
		setupMp3List();
		pawnChoiceInput = new SearchChoiceInput2(mp3TitleList);
		pawnChoiceInput.addListener(new PawnChoiceListener());
	}

	private void setupMp3List() {
		try {
			FileConnection fc = (FileConnection) Connector.open(PAWNED_FOLDER);
			Enumeration e = fc.list("*.pawn", true);
			int count = 0;

			while (e.hasMoreElements()) {
				e.nextElement();
				count++;
			}

			mp3TitleList = new String[count];
			mp3List = new Mp3File[count];
			e = fc.list("*.pawn", true);
			int index = 0;

			while (e.hasMoreElements()) {
				String fileName = (String) e.nextElement();
				String title = fileName.substring(0, fileName.lastIndexOf('.'));
				Mp3File mp3File = getMp3FileFromPawn(fileName);
				mp3TitleList[index] = title;
				mp3List[index] = mp3File;
				index++;
			}
			fc.close();
		} catch (IOException e) {
			Boot.alertException(e, this, "setupMp3List()");
		} catch (NoDataException e) {
			Boot.alertException(e, this, "setupMp3List()");
		}
	}

	private Mp3File getMp3FileFromPawn(String fileName) throws NoDataException {

		Mp3File result = null;
		try {
			FileConnection fc = (FileConnection) Connector.open(PAWNED_FOLDER
					+ "/" + fileName);
			InputStream fis = fc.openInputStream();
			int bufferLength = 512;
			byte[] b = new byte[bufferLength];
			int length = fis.read(b, 0, bufferLength);

			if (length > 0) {
				String filePath = new String(b, 0, length);
				// filePath = new String(b, 0, length, "ISO-8859-1");
				result = new Mp3File(filePath);
			} else {
				throw new NoDataException();
			}
			fis.close();
			fc.close();
		} catch (IOException e) {
			Boot.alertException(e, this, "getMp3FileFromPawn()");
		}
		return result;
	}

	private class PawnChoiceListener implements EventListener {

		public void processEvent(String event) {
			if (EventListener.EVENT_PROCESS_DONE.equals(event)) {
				int chosenIndex = pawnChoiceInput.getchosenIndex();
				Mp3File chosenMp3 = mp3List[chosenIndex];
				Boot.getInstance().enqueueMp3(chosenMp3);
				pawnChoiceInput.alert(chosenMp3.getTitle());
			} else if (EventListener.EVENT_REPAINT_REQUIRED.equals(event)) {
				repaint();
			}
		}

	}
}
